<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\App;

use think\exception\ValidateException;
use think\facade\Log;
use util\Excel;
use think\facade\Db;
use think\cache\driver\Redis;
use app\dic\DicOrder;

use util\Token;
/**
 * Personreport控制器
 */
class Personreport extends AdminBase
{

    protected $model = null;
    protected $ztotal_price = 0;
    protected $yztotal_price = 0;
    protected $zfreight = 0;
    protected $zmaterial = 0;
    protected $zfreight_addition = 0;
    protected $disputez = 0;
    protected $advertisingz = 0;

    protected $ztotal_price_old = 0;
    protected $zfreight_old = 0;
    protected $zmaterial_old = 0;
    protected $zfreight_addition_old = 0;
    protected $disputez_old = 0;
    protected $advertisingz_old = 0;

    protected $ztotal_price_new = 0;
    protected $zfreight_new = 0;
    protected $zmaterial_new = 0;
    protected $zfreight_addition_new = 0;
    protected $disputez_new = 0;
    protected $advertisingz_new = 0;

    protected $ztotal_price_main = 0;
    protected $zfreight_main = 0;
    protected $zmaterial_main = 0;
    protected $zfreight_addition_main = 0;
    protected $disputez_main = 0;
    protected $advertisingz_main = 0;

    protected $ztotal_price_secondary = 0;
    protected $zfreight_secondary = 0;
    protected $zmaterial_secondary = 0;
    protected $zfreight_addition_secondary = 0;
    protected $disputez_secondary = 0;
    protected $advertisingz_secondary = 0;

    protected $ztotal_price_dan = 0;
    protected $zfreight_dan = 0;
    protected $zmaterial_dan = 0;
    protected $zfreight_addition_dan = 0;
    protected $disputez_dan = 0;
    protected $advertisingz_dan = 0;

    protected $ztotal_price_10 = 0;
    protected $zfreight_10 = 0;
    protected $zmaterial_10 = 0;
    protected $zfreight_addition_10 = 0;
    protected $disputez_10 = 0;
    protected $advertisingz_10 = 0;

    protected $ztotal_price_29 = 0;
    protected $zfreight_29 = 0;
    protected $zmaterial_29 = 0;
    protected $zfreight_addition_29 = 0;
    protected $disputez_29 = 0;
    protected $advertisingz_29 = 0;

    protected $ztotal_price_20 = 0;
    protected $zfreight_20 = 0;
    protected $zmaterial_20 = 0;
    protected $zfreight_addition_20 = 0;
    protected $disputez_20 = 0;
    protected $advertisingz_20 = 0;

    protected $order_waiting = array();

    protected $ztotal_price_w = 0;
    protected $zfreight_w = 0;
    protected $zmaterial_w = 0;
    protected $zfreight_addition_w = 0;
    protected $disputez_w = 0;
    protected $advertisingz_w = 0;

    protected $exchange = 0;
    protected $exchange_eur = 0;

    protected $freightx_num1 = 0;
    protected $freightx_num2 = 0;

    protected $validate = null;

    protected $relationSearch = false;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Personreport();

        $this->validate = \app\admin\validate\Personreport::class;
    }

    /**
     * 列表
     */
    public function index()
    {

        list($limit, $where, $sortArr) = $this->buildTableParames();

        $where2 = array();
        $filters = $this->request->get('filter', '{}');
        $filters = json_decode($filters, true);
        //筛选日期
        if(!empty($filters['month']) && isset($filters['month'])){
            $where2['timedate'] = strtotime($filters['month']);
        }
        if(!empty($filters['admin_id']) && isset($filters['admin_id'])){
            $where2[] = ['admin_id', '=', $filters['admin_id']];
        }
        if(!empty($filters['team_id']) && isset($filters['team_id'])){
            switch ($filters['team_id']) {
                case '所有':
                    $team_id = '0';
                    break;
                case '武汉':
                    $team_id = '1';
                    break;
                case '南昌':
                    $team_id = '2';
                    break;
                case '银川':
                    $team_id = '3';
                    break;
                default:
                    $team_id = '0';
            }
            if($team_id > 0){
                $where2[] = ['b.team_id', '=', $team_id];
            }
        }

        $token = $this->request->header('token');
        $admin = Token::admin($token);
        if(!empty($admin)){
            $group_admin = table('system_group_admin')
            ->where('group_id', 5)
            ->where('admin_id', $admin['id'])
            ->find();
            if(!empty($group_admin)){
                //是运营
                $where2['admin_id'] = $admin['id'];
            }
        }
        $list = $this->model
            ->field("a.*")
            ->alias('a')
            ->join('qu_system_admin b', 'b.id = a.admin_id', 'left')
            ->where($where2)
            ->order('timedate desc,profit desc')
            ->paginate($limit);

        $zprofit = $this->model
            ->alias('a')
            ->join('qu_system_admin b', 'b.id = a.admin_id', 'left')
            ->where($where2)
            ->sum('profit');

        $ztotal_price = $this->model
            ->alias('a')
            ->join('qu_system_admin b', 'b.id = a.admin_id', 'left')
            ->where($where2)
            ->sum('total_price');
        $yztotal_price = $this->model
            ->alias('a')
            ->join('qu_system_admin b', 'b.id = a.admin_id', 'left')
            ->where($where2)
            ->sum('ytotal_price');

        $lists = $list->items();

        //店铺
        $msg = "";
        $vs_total_price = 0;
        if(!empty($where2['timedate']) && isset($where2['timedate'])){
            $where_timedate2 = $where2['timedate'] + 31*3600*24;
            $where1 = array();
            $where1[]  = ['a.created_at', '>=', $where2['timedate']];
            $where1[]  = ['a.created_at', '<=', $where_timedate2];
            $where_total_price[] = ['a.financial_status', 'in', [DicOrder::$financial_status_partially_paid, DicOrder::$financial_status_paid, DicOrder::$financial_status_partially_refunded, DicOrder::$financial_status_refunded]];
            $site_array = table('order')
                ->field('site_id,sum(total_price) as total_price,b.site_name,a.currency')
                ->alias('a')
                ->join('qu_site b','b.id = a.site_id', 'left')
                ->where($where1)
                ->where($where_total_price)
                ->where('a.status','normal')
                ->group('a.site_id')
                ->select()->toarray();
            
            $where2_timedate = date('Y-m', $where2['timedate']);
            $exchange = table('exchange')
                ->where('currency','USD')
                ->where('date',$where2_timedate)
                ->find();
            $exchange_eur = table('exchange')
                ->where('currency','EUR')
                ->where('date',$where2_timedate)
                ->find();
            if(!empty($exchange) && !empty($exchange_eur)){
                foreach ($site_array as $ks => $vs) {
                    $site_log = table('site_log')
                        ->where('createtime', '<=', $where2['timedate'])
                        ->where('updatetime', '>=', $where2['timedate'])
                        ->where('site_id', '>=', $vs['site_id'])
                        ->find();
                    if(empty($site_log)){
                        switch ($vs['currency']) {
                            case 'USD':
                                $vs['currency'] = '$';
                                break;
                            case 'EUR':
                                $vs['currency'] = '€';
                                $vs['total_price'] = (float)number_format((float)$vs['total_price'] * (float)$exchange_eur['proportion'] / (float)$exchange['proportion'], 3, '.', '');
                                break;
                            default:
                                $value['currency'] = '未知';
                        }
    
                        if(!empty($msg)){
                            $msg = $msg.',店铺：'.$vs['site_name'].'销售额：'.$vs['currency'].''.$vs['total_price'];
                        }else{
                            $msg = '店铺：'.$vs['site_name'].'销售额：'.$vs['currency'].''.$vs['total_price'];
                        }
    
                        $vs_total_price = (float)number_format((float)$vs_total_price + $vs['total_price'], 3, '.', '');
                    }
                }
            }else{
                $msg = $where2_timedate.'汇率没有记录';
            }
            if(!empty($vs_total_price)){
                $msg = $msg.'。无人运营店铺总销售额：$'.$vs_total_price;
                $yztotal_price2 = (float)number_format((float)$yztotal_price + $vs_total_price, 3, '.', '');
                $msg = $msg.'。总销售额：$'.$yztotal_price2;
            }
        }
        foreach ($lists as $k => $v) {
            $admin = table('system_admin')
                ->where(['id' => $v['admin_id']])->find();
            if (!empty($admin)) {
                $lists[$k]['admin_id'] = $admin['nickname'];
            } else {
                $lists[$k]['admin_id'] = '未知';
            }
            $lists[$k]['timedate'] = date('Y-m', $v['timedate']);
            switch ($v['performance']) {
                case '0':
                    $lists[$k]['performance'] = '未归档';
                    break;
                case '1':
                    $lists[$k]['performance'] = '已归档';
                    break;
                default:
                $lists[$k]['performance'] = '未归档';
            }
            
        }

        //时间
        $thismonth = date('m');
        $thisyear = date('Y');
        if ($thismonth == 1) {
            $lastmonth = 11;
            $lastyear = $thisyear - 1;
        }elseif($thismonth == 2){
            $lastmonth = 12;
            $lastyear = $thisyear - 1;
        } else {
            $lastmonth = $thismonth - 2;
            $lastyear = $thisyear;
        }
        $lastStartDay = $lastyear . '-' . $lastmonth;

        //统计
        $timedate = strtotime(date('Y-m-1', time()));
        $logistics_count = Db::name('order_logistics')
            ->where('status','hidden')
            ->where('updatetime','<',1)
            ->where('timedate',$timedate)
            ->count();
        $dispute_count = Db::name('order_dispute_manual')
            ->where('status','hidden')
            ->where('timedate',$timedate)
            ->count();

        $lastStartDay2 = $lastyear . '-' . $lastmonth . '-1';
        $lastEndDay = $lastyear . '-' . $lastmonth . '-' . date('t', strtotime($lastStartDay2)) . ' 23:59:59';
        $b_time = strtotime($lastStartDay2); //上个月的月初时间戳
        $e_time = strtotime($lastEndDay); //上个月的月末时间戳
        $where = array();
        $where[] = ['created_at', '>=', $b_time];
        $where[] = ['created_at', '<=', $e_time];
        $where[] = ['financial_status', 'in', ['paid', 'refunded', 'partially_refunded','partially_paid']];
        $ismaterial_count = Db::name('order')
            ->where('ismaterial',0)
            ->where($where)
            ->count();

        $logistics_countz = Db::name('order_logistics')
            ->where('timedate',$timedate)
            ->count();
        $ismaterial_countz = Db::name('order')
            ->where($where)
            ->count();
        $dispute_countz = Db::name('order_dispute_manual')
            ->where('timedate',$timedate)
            ->count();

        $data = [
            'code' => 1,
            'msg' => '',
            'count' => $list->total(),
            'data' => $lists,
            'start_time' => $lastStartDay,
            'logistics_count' => $logistics_count,
            'ismaterial_count' => $ismaterial_count,
            'dispute_count' => $dispute_count,
            'logistics_countz' => $logistics_countz,
            'ismaterial_countz' => $ismaterial_countz,
            'dispute_countz' => $dispute_countz,
            'ztotal_price' => $ztotal_price,
            'yztotal_price' => $yztotal_price,
            'yztotal_price_msg' => $msg,
            'zprofit' => $zprofit,
        ];

        return json($data);
    }

    /**
     * 修改
     *
     */
    public function edit()
    {
        $id= input('id');
        $row = $this->model->find($id);
        if (empty($row)) {
            return error('数据不存在');
        }

        if ($this->request->isPost()) {
            $post = $this->request->post();
            try {
                $this->validate && validate($this->validate)->check($post);
                if(empty($post['advertising'])){
                    $post['advertising'] = 0;
                }
                $post['profit'] = (float)number_format((float)$row['advertising'] + (float)$row['profit'] - (float)$post['advertising'], 3, '.', '');
                $result = $row->save($post);
                if ($result) {
                    return success('保存成功！');
                }
                return error('保存失败');
            } catch (ValidateException $e) {

                return error($e->getError());
            } catch (\Exception $e) {
                Log::error("--------:" . $e);
                return error('保存失败');
            }
        }
        return success('ok', $row);
    }
    /**
     * 每日业绩
     *
     */
    // public function edit()
    // {
    //     $id= input('id');
    //     $row = $this->model->find($id);
    //     $where_admin['admin_id'] = $row['admin_id'];

    //     $start_time = $row['timedate'];
    //     $firstday = date("Y-m-d", $row['timedate']);
    //     $end_time = strtotime(date("Y-m-d", strtotime("$firstday +1 month")));
    //     $where_day[]  = ['time_stamp', '>=', $start_time];
    //     $where_day[]  = ['time_stamp', '<', $end_time];
        
    //     $row2 = table('shopify_personreport_day')
    //         ->where($where_day)
    //         ->where($where_admin)
    //         ->order('timedate desc')
    //         ->select()
    //         ->toarray();
    //     foreach ($row2 as $k=>$value) {
    //         $advplatform = table('site')->where(['id' => $value['site_id']])->find();
    //         if (!empty($advplatform)) {
    //             $row2[$k]['site_name'] = $advplatform['site_name'];
    //         } else {
    //             $row2[$k]['site_name'] = '未知';
    //         }
    //     }
        
    //     $row3['admin_id'] = $row['admin_id'];

    //     $system_admin = table('system_admin')->where(['id' => $row['admin_id']])->find();
    //     $row3['admin_name'] = $system_admin['nickname'];

    //     $row3['start_time'] = $start_time;
    //     $row3['end_time'] = (string)$end_time;
    //     $row3['list'] = $row2;
    //     if (empty($row2)) {
    //         return error('数据不存在');
    //     }
    //     return success('ok', $row3);
    // }

    /**
     * 导出
     */
    public function export_personreport_day()
    {
        $get = $this->request->get();
        $where['admin_id'] = $get['admin_id'];
        $where[]  = ['time_stamp', '>=', $get['start_time']];
        $where[]  = ['time_stamp', '<', $get['end_time']];
        $list = table('shopify_personreport_day')
            ->field("timedate,site_id,label,total_price,count,freight,material,current_subtotal_price,current_total_discounts,current_total_tax,total_shipping_price_set,total_tip_received")
            ->where($where)
            ->order('timedate desc')
            ->select()
            ->toarray();
        foreach ($list as $k=>$value) {
            $advplatform = table('site')->where(['id' => $value['site_id']])->find();
            if (!empty($advplatform)) {
                $list[$k]['site_id'] = $advplatform['site_name'];
            } else {
                $list[$k]['site_id'] = '未知';
            }
        }

        $list_sum = table('shopify_personreport_day')
            ->field('
                sum(total_price) as total_price,
                sum(count) as count,
                sum(freight) as freight,
                sum(material) as material,
                sum(current_subtotal_price) as current_subtotal_price,
                sum(current_total_discounts) as current_total_discounts,
                sum(current_total_tax) as current_total_tax,
                sum(total_shipping_price_set) as total_shipping_price_set,
                sum(total_tip_received) as total_tip_received
            ')
            ->where($where)
            ->find();
        $list_sum2['timedate'] = '总计';
        $list_sum2['site_id'] = '';
        $list_sum2['label'] = '';
        $list_sum2['total_price'] = $list_sum['total_price'];
        $list_sum2['count'] = $list_sum['count'];
        $list_sum2['freight'] = $list_sum['freight'];
        $list_sum2['material'] = $list_sum['material'];
        $list_sum2['current_subtotal_price'] = $list_sum['current_subtotal_price'];
        $list_sum2['current_total_discounts'] = $list_sum['current_total_discounts'];
        $list_sum2['current_total_tax'] = $list_sum['current_total_tax'];
        $list_sum2['total_shipping_price_set'] = $list_sum['total_shipping_price_set'];
        $list_sum2['total_tip_received'] = $list_sum['total_tip_received'];

        $list[] = $list_sum2;

        $header[0] = ['日期', 'timedate'];
        $header[1] = ['店铺', 'site_id'];
        $header[2] = ['运营类型', 'label'];
        $header[3] = ['已发货销售额', 'total_price'];
        $header[4] = ['订单量', 'count'];
        $header[5] = ['物流$', 'freight'];
        $header[6] = ['采购$', 'material'];
        $header[7] = ['小计$', 'current_subtotal_price'];
        $header[8] = ['折扣$', 'current_total_discounts'];
        $header[9] = ['总税额$', 'current_total_tax'];
        $header[10] = ['总运费$', 'total_shipping_price_set'];
        $header[11] = ['小费$', 'total_tip_received'];
        $tableName = '每日业绩';
        $fileName = "export_" . $tableName . "_" . time();
        return Excel::exportData($list, $header, $fileName, 'xlsx');
    }

    /**
     * 导出
     */
    public function download_sales()
    {
        $redis = new Redis();
        $header = array();
        $header[] = ['日期', 'created_at'];
        $header[] = ['店铺', 'site_id'];
        $header[] = ['订单号', 'name'];
        $header[] = ['原销售额', 'total_price'];
        $header[] = ['物流$', 'freight'];
        $header[] = ['采购$', 'material'];
        $header[] = ['币种', 'currency'];
        $header[] = ['支付渠道', 'gateway'];
        $header[] = ['支付状态', 'financial_status'];
        $header[] = ['收货状态', 'fulfillment_status'];
        $header[] = ['标签', 'tags'];
        $header[] = ['争议状态', 'dispute_status'];
        $header[] = ['争议金额', 'dispute_cost'];
        $header[] = ['备注', 'remarks'];
        $header[] = ['现销售额', 'total_pricex'];
        $header[] = ['现运费', 'freightx'];
        $header[] = ['现采购', 'materialx'];
        $header[] = ['商品类型', 'commodity_type'];
        $header[] = ['退款金额', 'refunded'];
        $header[] = ['运费是否估算', 'estimate_freight'];
        $header[] = ['采购是否估算', 'estimate_material'];
        $tableName = '当月订单明细';
        $fileName = "export_" . $tableName . "_" . time();

        $get = $this->request->get();
        $where['admin_id'] = $get['id'];
        $shopify_personreport = table('shopify_personreport')
            ->where('id',$get['id'])
            ->find();
        $admin_id = $shopify_personreport['admin_id'];
        $b_time = $shopify_personreport['timedate'];

        $bb_time = date("Y-m-01",$b_time);
        $bb_time = strtotime("$bb_time -1 month");

        if($shopify_personreport['performance'] == 1){
            $list = $redis->get('personreport_performancelist-' . $admin_id .'-'.$b_time);
            if(!empty($list)){
                return Excel::exportData($list, $header, $fileName, 'xlsx');
            }
        }

        $firstday = date("Y-m-d", $shopify_personreport['timedate']);
        $e_time = strtotime(date("Y-m-d", strtotime("$firstday +1 month")));
        //计算业绩
        $where['admin_id'] = $admin_id;
        $where[]  = ['updatetime', '>=', $b_time];
        $where[]  = ['site_id', '<>', 121];
        $site_log =  table('site_log')
            ->where($where)
            ->select()->toarray();
        $list = array();

        //查看当月汇率
        $exchange_time = date("Y-m", $b_time);
        $exchange = $redis->get('exchangerate'.$exchange_time);
        if(empty($exchange)){
            $exchange = table('exchange')
                ->where('currency','USD')
                ->where('date',$exchange_time)
                ->find();
            if(empty($exchange)){
                $exchange = table('exchange')
                ->where('currency','USD')
                ->order('date desc')
                ->find();
            }  
            $redis->set('exchangerate'.$exchange_time, $exchange, 60 * 60);
        }

        $exchange_eur = $redis->get('exchangerate_eur'.$exchange_time);
        if(empty($exchange_eur)){
            $exchange_eur = table('exchange')
                ->where('currency','EUR')
                ->where('date',$exchange_time)
                ->find();
            if(empty($exchange_eur)){
                $exchange_eur = table('exchange')
                ->where('currency','EUR')
                ->order('date desc')
                ->find();
            }  
            $redis->set('exchangerate_eur'.$exchange_time, $exchange_eur, 60 * 60);
        }
        $this->exchange = $exchange;
        $this->exchange_eur = $exchange_eur;

         //FB广告总消耗
        $start_time_str = date('Y-m-d H:i:s', $b_time);
        $end_time_str =  date('Y-m-d H:i:s', $e_time);
        $adv_where[]  = ['create_time', '>=', $start_time_str];
        $adv_where[]  = ['create_time', '<=', $end_time_str];
        $where_admin['admin_id'] = $admin_id;
        # TODO z广告费规则
        $this->advertisingz = number_format(table('advertising')
        ->where($adv_where)
        ->where($where_admin)
        ->sum('cost'), 3, '.', '');

        
        table('order_waiting')
            ->where('time',$b_time)
            ->where('admin_id',$admin_id)
            ->delete();

        if(!empty($site_log)){
            foreach ($site_log as $k=>$v) {
                    //重新定义时间段
                if($v['createtime'] >= $b_time){
                    $start_time = $v['createtime'];
                }else{
                    $start_time = $b_time;
                }
                if($v['updatetime'] >= $e_time){
                    $updat_etime = $e_time;
                }else{
                    $updat_etime = $v['updatetime'];
                }
                if($v['type'] == '副运营'){
                    $wheretag = "AND tags like '%" . $v['label'] . "%'";
                    $sql = "
                        select 
                        id,
                        created_at,
                        site_id,
                        name,
                        total_price,
                        freight,
                        material,
                        refunded,
                        currency,
                        gateway,
                        financial_status,
                        fulfillment_status,
                        tags,
                        dispute_status,
                        dispute_cost,
                        istransfer as estimate_freight,
                        ismaterial as estimate_material,
                        new_product
                        from qu_order 
                        WHERE 
                        site_id = ".$v['site_id']."
                        AND (financial_status = 'paid' or financial_status = 'partially_refunded' or financial_status = 'refunded' or financial_status = 'partially_paid')
                        ".$wheretag."
                        AND created_at >= ".$start_time."
                        AND created_at <= ".$updat_etime."
                        AND status = 'normal'
                        ORDER BY created_at DESC";
                    $join_datas = Db::query($sql);
                    // 多标签的订单平分
                    $site_log2 =  table('site_log')
                        ->where('site_id',$v['site_id'])
                        ->where('type','副运营')
                        ->where('label', '<>', $v['label'])
                        ->where('admin_id', '<>', $v['admin_id'])
                        ->select()->toarray();

                    foreach ($join_datas as $item) {
                            $site_name = $redis->get('download_sales-' . $item['site_id']);
                            if(!empty($site_name)){
                                $item['site_id'] = $site_name;
                            }else{
                                $site = table('site')
                                ->where('id',$item['site_id'])
                                ->find();
                                if(!empty($site)){
                                    $site_name = $site['site_name'];
                                    $redis->set('download_sales-' . $item['site_id'], $site_name, 60 * 60);
                                    $item['site_id'] = $site_name;
                                }
                            }
                            $item['created_at'] = date('Y-m-d', $item['created_at']);
                            $item['freight'] = (float)number_format((float)$item['freight'] / (float)$exchange['proportion'], 3, '.', '');
                            $item['material'] = (float)number_format((float)$item['material'] / (float)$exchange['proportion'], 3, '.', '');
                            if(empty($item['refunded'])){
                                $item['refunded'] = 0;
                            }
                            $item = $this->handling_performance($item,2,$b_time,$v,$site_log2);
                            //吴靓莉的邮件业绩要分给主运营5%
                            if($admin_id == 31){
                                //LilyBras USA
                                // if($v['label'] == 'Omnisend' && $v['site_id'] == 10){
                                //     $this->ztotal_price_10 = (float)number_format((float)$item['total_pricex'] + $this->ztotal_price_10, 3, '.', '');
                                //     $this->zfreight_10 = (float)number_format((float)$item['freightx'] + $this->zfreight_10, 3, '.', '');
                                //     $this->zmaterial_10 = (float)number_format((float)$item['materialx'] + $this->zmaterial_10, 3, '.', '');
                                //     if($item['fulfillment_status'] == 'fulfilled'){
                                //         $this->zfreight_addition_10 += (float)number_format(2 / (float)$exchange['proportion'], 3, '.', '');
                                //     }
                                //     $this->disputez_10 = (float)number_format((float)$item['dispute_cost'] + $this->disputez_10, 3, '.', '');
                                // }
                                //WooBilly Bras
                                if($v['label'] == 'Omnisend' && $v['site_id'] == 29){
                                    $this->ztotal_price_29 = (float)number_format((float)$item['total_pricex'] + $this->ztotal_price_29, 3, '.', '');
                                    $this->zfreight_29 = (float)number_format((float)$item['freightx'] + $this->zfreight_29, 3, '.', '');
                                    $this->zmaterial_29 = (float)number_format((float)$item['materialx'] + $this->zmaterial_29, 3, '.', '');
                                    if($item['fulfillment_status'] == 'fulfilled'){
                                        $this->zfreight_addition_29 += (float)number_format(2 / (float)$exchange['proportion'], 3, '.', '');
                                    }
                                    $this->disputez_29 = (float)number_format((float)$item['dispute_cost'] + $this->disputez_29, 3, '.', '');
                                }
                                //Lily Bras
                                if($v['label'] == 'Omnisend' && $v['site_id'] == 109){
                                    $this->ztotal_price_20 = (float)number_format((float)$item['total_pricex'] + $this->ztotal_price_20, 3, '.', '');
                                    $this->zfreight_20 = (float)number_format((float)$item['freightx'] + $this->zfreight_20, 3, '.', '');
                                    $this->zmaterial_20 = (float)number_format((float)$item['materialx'] + $this->zmaterial_20, 3, '.', '');
                                    if($item['fulfillment_status'] == 'fulfilled'){
                                        $this->zfreight_addition_20 += (float)number_format(2 / (float)$exchange['proportion'], 3, '.', '');
                                    }
                                    $this->disputez_20 = (float)number_format((float)$item['dispute_cost'] + $this->disputez_20, 3, '.', '');
                                }
                            }
                            $list[] = $item;
                    }
                }
                if($v['type'] == '主运营'){
                    $sql = "
                    select 
                    created_at,
                    site_id,
                    name,
                    total_price,
                    freight,
                    material,
                    refunded,
                    currency,
                    gateway,
                    financial_status,
                    fulfillment_status,
                    tags,
                    dispute_status,
                    dispute_cost,
                    istransfer as estimate_freight,
                    ismaterial as estimate_material,
                    new_product
                    from qu_order 
                    WHERE 
                    site_id = ".$v['site_id']."
                    AND (financial_status = 'paid' or financial_status = 'partially_refunded' or financial_status = 'refunded' or financial_status = 'partially_paid')
                    AND created_at >= ".$start_time."
                    AND created_at <= ".$updat_etime."
                    AND status = 'normal'
                    ORDER BY created_at DESC";
                    $join_datas = Db::query($sql);
                    if(!empty($join_datas)){
                        $site_log2 =  table('site_log')
                                ->where('site_id',$v['site_id'])
                                ->where('type','副运营')
                                ->where('admin_id','<>',$v['admin_id'])
                                ->select()->toarray();
                        foreach ($join_datas as $item) {
                            if(!empty($site_log2)){
                                foreach ($site_log2 as $klog=>$vlog) {
                                    $pos = strpos($item['tags'], $vlog['label']);
                                    if($pos !== false){
                                        //找到
                                        unset($item);
                                        break;
                                    }
                                }
                            }
                            if(isset($item)){
                                $site_name = $redis->get('download_sales-' . $item['site_id']);
                                if(!empty($site_name)){
                                    $item['site_id'] = $site_name;
                                }else{
                                    $site = table('site')
                                    ->where('id',$item['site_id'])
                                    ->find();
                                    if(!empty($site)){
                                        $site_name = $site['site_name'];
                                        $redis->set('download_sales-' . $item['site_id'], $site_name, 60 * 60);
                                        $item['site_id'] = $site_name;
                                    }
                                }
                                $item['created_at'] = date('Y-m-d', $item['created_at']);
                                $item['freight'] = (float)number_format((float)$item['freight'] / (float)$exchange['proportion'], 3, '.', '');
                                $item['material'] = (float)number_format((float)$item['material'] / (float)$exchange['proportion'], 3, '.', '');
                                if(empty($item['refunded'])){
                                    $item['refunded'] = 0;
                                }
                                $item = $this->handling_performance($item,1,$b_time,$v);
                                if($item){
                                    $item['admin_id'] = $admin_id;
                                    $item['b_time'] = $b_time;
                                    $list[] = $item;
                                }
                            }
                        }
                    }
                }
            }
        }
        $list = $this->processing_tables($list,$admin_id,$b_time,$shopify_personreport);

        //查到上个月已付款(未发货)
        if(!empty($this->order_waiting)){
            table('order_waiting')->insertAll($this->order_waiting);
        }
        $order_waiting = table('order_waiting')
            ->where('time',$bb_time)
            ->where('admin_id',$admin_id)
            ->select();
        if(!empty($order_waiting)){
            $list_sumb = array();
            $list_sumb['created_at'] = '';
            $list_sumb['site_id'] = '';
            $list_sumb['name'] = '';
            $list_sumb['total_price'] = '';
            $list_sumb['freight'] = '';
            $list_sumb['material'] = '';
            $list_sumb['refunded'] = '';
            $list_sumb['currency'] = '';
            $list_sumb['gateway'] = '';
            $list_sumb['financial_status'] = '';
            $list_sumb['fulfillment_status'] = '';
            $list_sumb['tags'] = '';
            $list_sumb['dispute_status'] = '';
            $list_sumb['dispute_cost'] = '';
            $list_sumb['remarks'] = '';
            $list_sumb['total_pricex'] = '';
            $list_sumb['freightx'] = '';
            $list_sumb['materialx'] = '';
            $list_sumb['commodity_type'] = '';
            $list_sumb['estimate_freight'] = '';
            $list_sumb['estimate_material'] = '';
            $list_sumb['admin_id'] = $admin_id;
            $list_sumb['b_time'] = $b_time;
            $list[] = $list_sumb;
            $list[] = $list_sumb;
            $list[] = $list_sumb;

            foreach ($order_waiting as $kw=>$vw) {
                $item_w = table('order')
                    ->field('
                        created_at,
                        site_id,
                        name,
                        total_price,
                        freight,
                        material,
                        refunded,
                        currency,
                        gateway,
                        financial_status,
                        fulfillment_status,
                        tags,
                        dispute_status,
                        dispute_cost,
                        email,
                        shipping_name,
                        closed_at,
                        istransfer as estimate_freight,
                        ismaterial as estimate_material,
                        new_product
                    ')
                    ->where('name',$vw['name'])
                    ->where('site_id',$vw['site_id'])
                    ->find();
                if(!empty($item_w)){
                    $site_name = $redis->get('download_sales-' . $item_w['site_id']);
                    if(!empty($site_name)){
                        $item_w['site_id'] = $site_name;
                    }else{
                        $site = table('site')
                        ->where('id',$item_w['site_id'])
                        ->find();
                        if(!empty($site)){
                            $site_name = $site['site_name'];
                            $redis->set('download_sales-' . $item_w['site_id'], $site_name, 60 * 60);
                            $item_w['site_id'] = $site_name;
                        }
                    }
                    if(!empty($item_w['created_at'])){
                        $item_w['created_at'] = date('Y-m-d', $item_w['created_at']);
                    }
                    $item_w['freight'] = (float)number_format((float)$item_w['freight'] / (float)$exchange['proportion'], 3, '.', '');
                    $item_w['material'] = (float)number_format((float)$item_w['material'] / (float)$exchange['proportion'], 3, '.', '');
                    if(empty($item_w['refunded'])){
                        $item_w['refunded'] = 0;
                    }
                    $vw = $this->handling_performance($item_w,3,$b_time,$vw);
                    if($vw){
                        $vw['admin_id'] = $admin_id;
                        $vw['b_time'] = $b_time;
                        $vw['commodity_type'] = $item_w['email'];
                        $vw['refunded'] = $item_w['shipping_name'];
                        $list[] = $vw;
                    }
                }
            }
            $list = $this->processing_tables_w($list,$admin_id,$b_time);
        }
        
        if($shopify_personreport['performance'] == 1){
            $redis->set('personreport_performancelist-' . $admin_id .'-'.$b_time, $list, 60*60*24*30);
        }
        return Excel::exportData($list, $header, $fileName, 'xlsx');
    }
    /**
     * 导出
     */
    public function download_achievement()
    {
        $list = array();
        $redis = new Redis();
        
        $lastmonth_start = mktime(0, 0, 0, date('m') - 2, 1, date('Y'));
        $lastmonth_end = mktime(23, 59, 59, date('m') - 1, 0, date('Y'));
        //查看当月汇率
        $exchange_time = date("Y-m", $lastmonth_start);
        $exchange = $redis->get('exchangerate'.$exchange_time);
        if(empty($exchange)){
            $exchange = table('exchange')
                ->where('currency','USD')
                ->where('date',$exchange_time)
                ->find();
            if(empty($exchange)){
                $exchange = table('exchange')
                ->where('currency','USD')
                ->order('date desc')
                ->find();
            }  
            $redis->set('exchangerate'.$exchange_time, $exchange, 60 * 60);
        }

        //六月lily店
        $sql = "
            select 
            created_at,
            site_id,
            name,
            total_price,
            freight,
            material,
            currency,
            gateway,
            financial_status,
            fulfillment_status,
            tags,
            dispute_status,
            dispute_cost
            from qu_order 
            WHERE 
            site_id = 10
            AND (financial_status = 'paid' or financial_status = 'partially_refunded' or financial_status = 'refunded' or financial_status = 'partially_paid')
            AND created_at >= ".$lastmonth_start."
            AND created_at <= ".$lastmonth_end."
            AND status = 'normal'
            ORDER BY created_at DESC";
            $join_datas = Db::query($sql);
                if(!empty($join_datas)){
                    $site_log2 =  table('site_log')
                            ->where('site_id',10)
                            ->where('type','副运营')
                            ->select()->toarray();
                    $ztotal_price_secondary = 0;
                    $zfreight_secondary = 0;
                    $zmaterial_secondary = 0;
                    $zfreight_addition_secondary = 0;
                    $disputez_secondary = 0;
                    foreach ($join_datas as $item) {
                        if(!empty($site_log2)){
                            foreach ($site_log2 as $klog=>$vlog) {
                                $pos = strpos($item['tags'], $vlog['label']);
                                if($pos !== false){
                                    //找到
                                    unset($item);
                                    break;
                                }
                            }
                        }
                        if(isset($item)){
                            $site_name = $redis->get('download_sales-' . $item['site_id']);
                            if(!empty($site_name)){
                                $item['site_id'] = $site_name;
                            }else{
                                $site = table('site')
                                ->where('id',$item['site_id'])
                                ->find();
                                if(!empty($site)){
                                    $site_name = $site['site_name'];
                                    $redis->set('download_sales-' . $item['site_id'], $site_name, 60 * 60);
                                    $item['site_id'] = $site_name;
                                }
                            }
                            $item['created_at'] = date('Y-m-d', $item['created_at']);
                            $item['freight'] = (float)number_format((float)$item['freight'] / (float)$exchange['proportion'], 3, '.', '');
                            $item['material'] = (float)number_format((float)$item['material'] / (float)$exchange['proportion'], 3, '.', '');

                            $item['total_pricex'] = 0;
                            $item['freightx'] = 0;
                            $item['materialx'] = 0;
                            $item['remarks'] = '';
                            if($item['tags'] == '暂无'){
                                $item['tags'] = '';
                            }
                            switch ($item['dispute_status']) {
                                case '0':
                                    $item['dispute_status'] = '正常';
                                    $item['total_pricex'] = $item['total_price'];
                                    $item['freightx'] = $item['freight'];
                                    $item['materialx'] = $item['material'];
                                    break;
                                case '1':
                                    $item['dispute_status'] = '有争议';
                                    $item['total_pricex'] = 0;
                                    $item['freightx'] = $item['freight'];
                                    $item['materialx'] = $item['material'];
                                    $item['remarks'] = '争议订单不算销售额';
                                    break;
                                default:
                                    $item['dispute_status'] = '未知';
                            }
                            if($item['dispute_status'] == '有争议' && $item['dispute_cost'] <= 0.01){
                                //默认争议费
                                switch ($item['gateway']) {
                                    case 'paypal':
                                        $item['dispute_cost'] = 16;
                                        break;
                                    case 'stripe':
                                        $item['dispute_cost'] = 18;
                                        break;
                                    default:
                                        $item['dispute_cost'] = 16;
                                }
                            }
                            if($item['financial_status'] == 'paid' && $item['fulfillment_status'] !== 'fulfilled'){
                                $item['total_pricex'] = 0;
                                $item['freightx'] = 0;
                                $item['materialx'] = 0;
                                $item['remarks'] = $item['remarks'].'|已付款(未发货)不算业绩';
                            }
                            if($item['financial_status'] == 'refunded' && $item['fulfillment_status'] !== 'fulfilled'){
                                $item['total_pricex'] = 0;
                                $item['freightx'] = 0;
                                $item['materialx'] = 0;
                                $item['remarks'] = $item['remarks'].'|已退款(未发货)不算业绩';
                            }
                            if($item['financial_status'] == 'refunded' && $item['fulfillment_status'] == 'fulfilled'){
                                $item['total_pricex'] = 0;
                                $item['freightx'] = $item['freight'];
                                $item['materialx'] = $item['material'];
                                $item['remarks'] = $item['remarks'].'|已退款(已发货)不算销售额';
                            }
                            $pos = strpos($item['tags'],'fb');
                            if($pos !== false){
                                unset($item);
                            }else{
                                // 平分
                                $ztotal_price_secondary = (float)number_format((float)$item['total_pricex'] + $ztotal_price_secondary, 3, '.', '');
                                $zfreight_secondary = (float)number_format((float)$item['freightx'] + $zfreight_secondary, 3, '.', '');
                                $zmaterial_secondary = (float)number_format((float)$item['materialx'] + $zmaterial_secondary, 3, '.', '');
                                if($item['fulfillment_status'] == 'fulfilled'){
                                    $zfreight_addition_secondary += (float)number_format(2 / (float)$exchange['proportion'], 3, '.', '');
                                }
                                $disputez_secondary = (float)number_format((float)$item['dispute_cost'] + $disputez_secondary, 3, '.', '');
                                $list[] = $item;
                            }
                        }
                    }
                }
            $new_data = array();
            $new_data['time'] = $lastmonth_start;
            $new_data['site_id'] = 10;
            $new_data['ztotal_price'] = $ztotal_price_secondary;//销售额
            $new_data['ztotal_price_secondary'] = (float)number_format((float)$ztotal_price_secondary * 0.94, 3, '.', '');
            $new_data['zfreight_secondary'] = $zfreight_secondary;
            $new_data['zmaterial_secondary'] = $zmaterial_secondary;
            $new_data['zfreight_addition_secondary'] = $zfreight_addition_secondary;//运费加成
            $new_data['disputez_secondary'] = $disputez_secondary;//争议
            //毛利
            $new_data['gross'] = (float)number_format($new_data['ztotal_price_secondary'] - $new_data['zfreight_secondary'] - $new_data['zmaterial_secondary'] - $new_data['zfreight_addition_secondary'] - $new_data['disputez_secondary'], 3, '.', '');
            $performance_equally = table('performance_equally')
                ->where('time',$lastmonth_start)
                ->where('site_id',10)
                ->find();
            if(!empty($performance_equally)){
                table('performance_equally')->where('id',$performance_equally['id'])->update($new_data);
            }else{
                table('performance_equally')->insert($new_data);
            }

            $list_sumb = array();
            $list_sumb['created_at'] = '';
            $list_sumb['site_id'] = '';
            $list_sumb['name'] = '';
            $list_sumb['total_price'] = '';
            $list_sumb['freight'] = '';
            $list_sumb['material'] = '';
            $list_sumb['currency'] = '';
            $list_sumb['gateway'] = '';
            $list_sumb['financial_status'] = '';
            $list_sumb['fulfillment_status'] = '';
            $list_sumb['tags'] = '';
            $list_sumb['dispute_status'] = '';
            $list_sumb['dispute_cost'] = '';
            $list_sumb['remarks'] = '';
            $list_sumb['total_pricex'] = '';
            $list_sumb['freightx'] = '';
            $list_sumb['materialx'] = '';
            $list[] = $list_sumb;
            $list[] = $list_sumb;
            $list[] = $list_sumb;

            $list_sum2 = array();
            $list_sum2['created_at'] = '统计';
            $list_sum2['site_id'] = '业绩类型';
            $list_sum2['name'] = '已发货销售额$';
            $list_sum2['total_price'] = '94%销售额$';
            $list_sum2['freight'] = '总物流$';
            $list_sum2['material'] = '总采购$';
            $list_sum2['currency'] = '总物流加成$';
            $list_sum2['gateway'] = '总广告成本$';
            $list_sum2['financial_status'] = '争议费$';
            $list_sum2['fulfillment_status'] = '盈利$';
            $list_sum2['tags'] = '运营';
            $list_sum2['dispute_status'] = '广告';
            $list_sum2['dispute_cost'] = '广告占比';
            $list_sum2['remarks'] = '最终分到的业绩';
            $list_sum2['total_pricex'] = '';
            $list_sum2['freightx'] = '';
            $list_sum2['materialx'] = '';
            $list[] = $list_sum2;

            $list_sum3 = array();
            $list_sum3['created_at'] = '';
            $list_sum3['site_id'] = '平分业绩总和';
            $list_sum3['name'] = $new_data['ztotal_price'];
            $list_sum3['total_price'] = $new_data['ztotal_price_secondary'];
            $list_sum3['freight'] = $new_data['zfreight_secondary'];
            $list_sum3['material'] = $new_data['zmaterial_secondary'];
            $list_sum3['currency'] = $new_data['zfreight_addition_secondary'];
            //FB广告总消耗
            $list_sum3['gateway'] = 0;
            $list_sum3['financial_status'] = $new_data['disputez_secondary'];
            $list_sum3['fulfillment_status'] = $new_data['gross'];
            $list_sum3['tags'] = '';
            $list_sum3['dispute_status'] = '';
            $list_sum3['dispute_cost'] = '';
            $list_sum3['remarks'] = '';
            $list_sum3['total_pricex'] = '';
            $list_sum3['freightx'] = '';
            $list_sum3['materialx'] = '';
            $list[] = $list_sum3;

            $list[] = $list_sumb;
            
            $performance_user = table('performance_user')
                ->where('site_id',10)
                ->where('time',$lastmonth_start)
                ->select()->toarray();
            foreach ($performance_user as $kp=>$vp) {
                $list_sum4 = array();
                $list_sum4['created_at'] = '';
                $list_sum4['site_id'] = '';
                $list_sum4['name'] = '';
                $list_sum4['total_price'] = '';
                $list_sum4['freight'] = '';
                $list_sum4['material'] = '';
                $list_sum4['currency'] = '';
                $list_sum4['gateway'] = '';
                $list_sum4['financial_status'] = '';
                $list_sum4['fulfillment_status'] = '';
                $list_sum4['tags'] = $vp['user'];
                $list_sum4['dispute_status'] = $vp['advertisement'];
                $list_sum4['dispute_cost'] = $vp['proportion'];
                $list_sum4['remarks'] = $vp['user_gross'];
                $list_sum4['total_pricex'] = '';
                $list_sum4['freightx'] = '';
                $list_sum4['materialx'] = '';
                $list[] = $list_sum4;
            }

            $header = array();
            $header[] = ['日期', 'created_at'];
            $header[] = ['店铺', 'site_id'];
            $header[] = ['订单号', 'name'];
            $header[] = ['原销售额$', 'total_price'];
            $header[] = ['物流$', 'freight'];
            $header[] = ['采购$', 'material'];
            $header[] = ['币种', 'currency'];
            $header[] = ['支付渠道', 'gateway'];
            $header[] = ['支付状态', 'financial_status'];
            $header[] = ['收货状态', 'fulfillment_status'];
            $header[] = ['标签', 'tags'];
            $header[] = ['争议状态', 'dispute_status'];
            $header[] = ['争议金额', 'dispute_cost'];
            $header[] = ['备注', 'remarks'];
            $header[] = ['现销售额', 'total_pricex'];
            $header[] = ['现运费', 'freightx'];
            $header[] = ['现采购', 'materialx'];

            $tableName = '当月平分业绩明细';
            $fileName = "export_" . $tableName . "_" . time();
            return Excel::exportData($list, $header, $fileName, 'xlsx');
            
    }
    /**
     * 处理业绩规则
     */
    public function handling_performance($item,$type,$b_time,$site,$site_log = array())
    {
        $item['total_pricex'] = 0;
        $item['freightx'] = 0;
        $item['materialx'] = 0;
        $item['remarks'] = '';
        $item['commodity_type'] = '';//商品类型
        if($item['tags'] == '暂无'){
            $item['tags'] = '';
        }
        switch ($item['dispute_status']) {
            case '0':
                $item['dispute_status'] = '正常';
                $item['total_pricex'] = (float)number_format((float)$item['total_price'] - $item['refunded'], 3, '.', '');
                $item['freightx'] = $item['freight'];
                $item['materialx'] = $item['material'];
                break;
            case '1':
                $item['dispute_status'] = '有争议';
                $item['total_pricex'] = 0;
                $item['freightx'] = $item['freight'];
                $item['materialx'] = $item['material'];
                $item['remarks'] = '争议订单不算销售额';
                break;
            default:
                $item['dispute_status'] = '未知';
        }
        if($item['dispute_status'] == '有争议' && $item['dispute_cost'] <= 0.01){
            //默认争议费
            switch ($item['gateway']) {
                case 'paypal':
                    $item['dispute_cost'] = 16;
                    break;
                case 'stripe':
                    $item['dispute_cost'] = 18;
                    break;
                default:
                    $item['dispute_cost'] = 16;
            }
        }
        if($item['financial_status'] == 'paid' && $item['fulfillment_status'] !== 'fulfilled'){
            $item['total_pricex'] = 0;
            $item['freightx'] = 0;
            $item['materialx'] = 0;
            $item['remarks'] = $item['remarks'].'|已付款(未发货)不算业绩';

            if($type !== 3){
                $waiting = array();
                $waiting['admin_id'] = $site['admin_id'];
                $waiting['time'] = $b_time;
                $waiting['name'] = $item['name'];
                $waiting['site_id'] = $site['site_id'];
                $this->order_waiting[] = $waiting;
            }
            
        }
        if($item['financial_status'] == 'refunded' && $item['fulfillment_status'] !== 'fulfilled'){
            $item['total_pricex'] = 0;
            $item['freightx'] = 0;
            $item['materialx'] = 0;
            $item['remarks'] = $item['remarks'].'|已退款(未发货)不算业绩';
        }
        if($item['financial_status'] == 'refunded' && $item['fulfillment_status'] == 'fulfilled'){
            $item['total_pricex'] = 0;
            $item['freightx'] = $item['freight'];
            $item['materialx'] = $item['material'];
            $item['remarks'] = $item['remarks'].'|已退款(已发货)不算销售额';
        }
        if(!empty($item['estimate_freight'])){
            // * 0未对接成功
            // * 1对接成功
            // * 2估算物流成功
            switch ($item['estimate_freight']) {
                case '0':
                    $item['estimate_freight'] = '等待估算';
                    $item['freightx'] = 0;
                    $this->freightx_num2 += 1;
                    break;
                case '1':
                    $item['estimate_freight'] = '真实运费';
                    $this->freightx_num1 += 1;
                    break;
                case '2':
                    $item['estimate_freight'] = '估算';
                    $this->freightx_num2 += 1;
                    $item['freightx'] = 0;
                    break;
                default:
                    $item['estimate_freight'] = '未知';
                    $this->freightx_num2 += 1;
                    $item['freightx'] = 0;
            }
            // * ismaterial 采购
            // * 0未对接成功
            // * 1对接成功
            // * 2预估
            switch ($item['estimate_material']) {
                case '0':
                    $item['estimate_material'] = '等待估算';
                    $item['materialx'] = 0;
                    break;
                case '1':
                    $item['estimate_material'] = '真实采购费';
                    break;
                case '2':
                    $item['estimate_material'] = '估算';
                    $item['materialx'] = 0;
                    break;
                default:
                    $item['estimate_material'] = '未知';
                    $item['materialx'] = 0;
            }
        }

        if($type == 3){
            if(!empty($item['closed_at'])){
                $item['remarks'] = $item['remarks'].'|已存档:'.$item['closed_at'];
            }
            if(isset($item)){
                // 计算新品标签
                // $this->calculate_new_product_labels($item);
                //总和
                $this->ztotal_price_w = (float)number_format((float)$item['total_pricex'] + $this->ztotal_price_w, 3, '.', '');
                $this->zfreight_w = (float)number_format((float)$item['freightx'] + $this->zfreight_w, 3, '.', '');
                $this->zmaterial_w = (float)number_format((float)$item['materialx'] + $this->zmaterial_w, 3, '.', '');
                if($item['fulfillment_status'] == 'fulfilled'){
                    $this->zfreight_addition_w += (float)number_format(2 / 7.150, 3, '.', '');
                }
                $this->disputez_w = 0;
                return $item;
            }else{
                return false;
            }
        }

        //多标签的订单平分
        if($type == 2){
            if(!empty($site_log)){
                foreach ($site_log as $klog=>$vlog) {
                    $pos = strpos($item['tags'], $vlog['label']);
                    if($pos !== false){
                        //找到 平分业绩

                        $redis = new Redis();
                        $admin_name = $redis->get('download_sales-admin_id-' . $vlog['admin_id']);
                        if(empty($admin_name)){
                            $admin = table('system_admin')
                            ->where('id',$vlog['admin_id'])
                            ->find();
                            if(!empty($admin)){
                                $admin_name = $admin['nickname'];
                                $redis->set('download_sales-admin_id-' . $vlog['admin_id'], $admin_name, 60 * 60);
                            }
                        }

                        $item['remarks'] = $item['remarks'].'|该订单有'.$vlog['label'].'标签,跟'.$admin_name.'平分业绩';
                        $item['total_pricex'] = (float)number_format((float)$item['total_pricex'] / 2, 3, '.', '');
                        $item['freightx'] = (float)number_format((float)$item['freightx'] / 2, 3, '.', '');
                        $item['materialx'] = (float)number_format((float)$item['materialx'] / 2, 3, '.', '');

                        //平分打上标签
                        $order_update = array();
                        $order_update['disputestatus'] = 'normal';
                        table('order')
                            ->where('id',$item['id'])
                            ->update($order_update);

                        break;
                    }
                }
            }
        }
        //多标签的订单平分
        if($type == 1){
            $a = 1;
            if(!empty($site_log['label']) && $site_log['site_id'] == 10 && $a == 2){
                $pos = strpos($item['tags'],$site_log['label']);
                if($pos !== false){
                    // 主运营标签
                    $this->ztotal_price_main = (float)number_format((float)$item['total_pricex'] + $this->ztotal_price_main, 3, '.', '');
                    $this->zfreight_main = (float)number_format((float)$item['freightx'] + $this->zfreight_main, 3, '.', '');
                    $this->zmaterial_main = (float)number_format((float)$item['materialx'] + $this->zmaterial_main, 3, '.', '');
                    // if($item['fulfillment_status'] == 'fulfilled'){
                    //     $this->zfreight_addition_main += (float)number_format(2 / 7.150, 3, '.', '');
                    // }
                    $this->disputez_main = (float)number_format((float)$item['dispute_cost'] + $this->disputez_main, 3, '.', '');

                    //六月计算新品标签
                    $item = $this->calculate_new_product_labels($item);
                }else{
                    unset($item);
                }
            }else{
                // 主运营标签
                $this->ztotal_price_main = (float)number_format((float)$item['total_pricex'] + $this->ztotal_price_main, 3, '.', '');
                $this->zfreight_main = (float)number_format((float)$item['freightx'] + $this->zfreight_main, 3, '.', '');
                $this->zmaterial_main = (float)number_format((float)$item['materialx'] + $this->zmaterial_main, 3, '.', '');
                // if($item['fulfillment_status'] == 'fulfilled'){
                //     $this->zfreight_addition_main += (float)number_format(2 / 7.150, 3, '.', '');
                // }
                $this->disputez_main = (float)number_format((float)$item['dispute_cost'] + $this->disputez_main, 3, '.', '');
                //六月计算新品标签
                $item = $this->calculate_new_product_labels($item);
            }
            // else{
            //     //六月单人店铺
            //     $this->ztotal_price_dan = (float)number_format((float)$item['total_pricex'] + $this->ztotal_price_dan, 3, '.', '');
            //     $this->zfreight_dan = (float)number_format((float)$item['freightx'] + $this->zfreight_dan, 3, '.', '');
            //     $this->zmaterial_dan = (float)number_format((float)$item['materialx'] + $this->zmaterial_dan, 3, '.', '');
            //     if($item['fulfillment_status'] == 'fulfilled'){
            //         $this->zfreight_addition_dan += (float)number_format(2 / 7.150, 3, '.', '');
            //     }
            //     $this->disputez_dan = (float)number_format((float)$item['dispute_cost'] + $this->disputez_dan, 3, '.', '');
            // }
        }
        if(isset($item)){
            // 计算新品标签
            // $this->calculate_new_product_labels($item);
            //总和
            //先判断币种
            if($item['currency'] == 'EUR'){
                $total_price_eur = (float)number_format((float)$item['total_price'] * (float)$this->exchange_eur['proportion'] / (float)$this->exchange['proportion'], 3, '.', '');
                $total_pricex_eur = (float)number_format((float)$item['total_pricex'] * (float)$this->exchange_eur['proportion'] / (float)$this->exchange['proportion'], 3, '.', '');
            }else{
                $total_price_eur = $item['total_price'];
                $total_pricex_eur = $item['total_pricex'];
            }
            
            $this->yztotal_price = (float)number_format((float)$total_price_eur + $this->yztotal_price, 3, '.', '');
            $this->ztotal_price = (float)number_format((float)$total_pricex_eur + $this->ztotal_price, 3, '.', '');
            $this->zfreight = (float)number_format((float)$item['freightx'] + $this->zfreight, 3, '.', '');
            $this->zmaterial = (float)number_format((float)$item['materialx'] + $this->zmaterial, 3, '.', '');
            if($item['fulfillment_status'] == 'fulfilled'){
                $this->zfreight_addition += (float)number_format(2 / 7.150, 3, '.', '');
            }
            $this->disputez = (float)number_format((float)$item['dispute_cost'] + $this->disputez, 3, '.', '');
            return $item;
        }else{
            return false;
        }
    }

    
    /**
    * 方法详情
    * @param array params
    */
    public function processing_tables_w($list,$admin_id,$b_time)
    {
        $list_sumb = array();
        $list_sumb['created_at'] = '';
        $list_sumb['site_id'] = '';
        $list_sumb['name'] = '';
        $list_sumb['total_price'] = '';
        $list_sumb['freight'] = '';
        $list_sumb['material'] = '';
        $list_sumb['refunded'] = '';
        $list_sumb['currency'] = '';
        $list_sumb['gateway'] = '';
        $list_sumb['financial_status'] = '';
        $list_sumb['fulfillment_status'] = '';
        $list_sumb['tags'] = '';
        $list_sumb['dispute_status'] = '';
        $list_sumb['dispute_cost'] = '';
        $list_sumb['remarks'] = '';
        $list_sumb['total_pricex'] = '';
        $list_sumb['freightx'] = '';
        $list_sumb['materialx'] = '';
        $list_sumb['commodity_type'] = '';
        $list_sumb['estimate_freight'] = '';
        $list_sumb['estimate_material'] = '';
        $list_sumb['admin_id'] = $admin_id;
        $list_sumb['b_time'] = $b_time;

        $list[] = $list_sumb;

        $list_sum3 = array();
        $list_sum3['created_at'] = '上个月结转';
        $list_sum3['site_id'] = '';
        $list_sum3['name'] = $this->ztotal_price_w;
        $list_sum3['total_price'] = (float)number_format((float)$this->ztotal_price_w * 0.94, 3, '.', '');
        $list_sum3['freight'] = $this->zfreight_w;
        $list_sum3['material'] = $this->zmaterial_w;
        $list_sum3['refunded'] = '';
        $list_sum3['currency'] = (float)$this->zfreight_addition_w;
        $list_sum3['gateway'] = 0;
        $list_sum3['financial_status'] = $this->disputez_w;
        $list_sum3['fulfillment_status'] = (float)number_format($list_sum3['total_price'] - $list_sum3['freight'] - $list_sum3['material'] - $list_sum3['currency'] - $list_sum3['gateway'] - $list_sum3['financial_status'], 3, '.', '');
        $list_sum3['tags'] = 0;
        $list_sum3['dispute_status'] = (float)number_format($list_sum3['fulfillment_status']+ $list_sum3['tags'], 3, '.', '');
        $list_sum3['dispute_cost'] = '';
        $list_sum3['remarks'] = '';
        $list_sum3['total_pricex'] = '';
        $list_sum3['freightx'] = '';
        $list_sum3['materialx'] = '';
        $list_sum3['commodity_type'] = '';
        $list_sum3['estimate_freight'] = '';
        $list_sum3['estimate_material'] = '';
        $list_sum3['admin_id'] = $admin_id;
        $list_sum3['b_time'] = $b_time;
        $list[] = $list_sum3;

        $list[] = $list_sumb;
        return $list;
    }

    /**
    * 方法详情
    * @param array params
    */
    public function processing_tables($list,$admin_id,$b_time,$shopify_personreport)
    {
        $performance_user = table('performance_user')
            ->where('site_id',10)
            ->where('time',$b_time)
            ->where('admin_id',$admin_id)
            ->find();
            if(!empty($performance_user)){
                $user_gross = $performance_user['user_gross'];
            }else{
                $user_gross = 0;
            }
        $list_sumb = array();
        $list_sumb['created_at'] = '';
        $list_sumb['site_id'] = '';
        $list_sumb['name'] = '';
        $list_sumb['total_price'] = '';
        $list_sumb['freight'] = '';
        $list_sumb['material'] = '';
        $list_sumb['refunded'] = '';
        $list_sumb['currency'] = '';
        $list_sumb['gateway'] = '';
        $list_sumb['financial_status'] = '';
        $list_sumb['fulfillment_status'] = '';
        $list_sumb['tags'] = '';
        $list_sumb['dispute_status'] = '';
        $list_sumb['dispute_cost'] = '';
        $list_sumb['remarks'] = '';
        $list_sumb['total_pricex'] = '';
        $list_sumb['freightx'] = '';
        $list_sumb['materialx'] = '';
        $list_sumb['commodity_type'] = '';
        $list_sumb['estimate_freight'] = '';
        $list_sumb['estimate_material'] = '';
        $list_sumb['admin_id'] = $admin_id;
        $list_sumb['b_time'] = $b_time;

        $list[] = $list_sumb;
        $list[] = $list_sumb;
        $list[] = $list_sumb;

        $list_sum2 = array();
        $list_sum2['created_at'] = '统计';
        $list_sum2['site_id'] = '个人业绩$';
        $list_sum2['name'] = '已发货销售额$';
        $list_sum2['total_price'] = '94%销售额$';
        $list_sum2['freight'] = '总物流$';
        $list_sum2['material'] = '总采购$';
        $list_sum2['refunded'] = '';
        $list_sum2['currency'] = '总物流加成$';
        $list_sum2['gateway'] = '总广告成本$';
        $list_sum2['financial_status'] = '争议费$';
        $list_sum2['fulfillment_status'] = '盈利$';
        $list_sum2['tags'] = '附加平分的销售额';
        $list_sum2['dispute_status'] = '最终盈利';
        $list_sum2['dispute_cost'] = '欧元汇率';
        $list_sum2['remarks'] = '美金汇率';
        $list_sum2['total_pricex'] = '真实物流费';
        $list_sum2['freightx'] = '估算物流费(取真实物流费的平均数)';
        $list_sum2['materialx'] = '估算平均数';
        $list_sum2['commodity_type'] = '估算单量';
        $list_sum2['estimate_freight'] = '';
        $list_sum2['estimate_material'] = '';
        $list_sum2['admin_id'] = $admin_id;
        $list_sum2['b_time'] = $b_time;
        $list[] = $list_sum2;

        $list_sum3 = array();
        $list_sum3['created_at'] = '总和';
        $list_sum3['site_id'] = $this->yztotal_price;
        $list_sum3['name'] = $this->ztotal_price;
        $list_sum3['total_price'] = (float)number_format((float)$this->ztotal_price * 0.94, 3, '.', '');
        if($this->zfreight > 0 && $this->freightx_num1 > 0 && $this->freightx_num2 > 0){
            $list_sum3['freight'] = (float)number_format((float)$this->zfreight + (float)$this->zfreight / (float)$this->freightx_num1 * (float)$this->freightx_num2, 3, '.', '');
        }else{
            $list_sum3['freight'] = $this->zfreight;
        }
        
        $list_sum3['material'] = $this->zmaterial;
        $list_sum3['refunded'] = '';
        $list_sum3['currency'] = (float)$this->zfreight_addition;

         //FB广告总消耗
        //手动改广告费qu_shopify_personreport
        if(!empty($shopify_personreport['advertising'])){
            $this->advertisingz = $shopify_personreport['advertising'];      
        }
        $list_sum3['gateway'] = $this->advertisingz;
        $list_sum3['financial_status'] = $this->disputez;
        $list_sum3['fulfillment_status'] = (float)number_format($list_sum3['total_price'] - $list_sum3['freight'] - $list_sum3['material'] - $list_sum3['currency'] - $list_sum3['gateway'] - $list_sum3['financial_status'], 3, '.', '');
        $list_sum3['tags'] = (float)$user_gross;
        $list_sum3['dispute_status'] = (float)number_format($list_sum3['fulfillment_status']+ $list_sum3['tags'], 3, '.', '');
        $list_sum3['dispute_cost'] = $this->exchange_eur['proportion'];
        $list_sum3['remarks'] = $this->exchange['proportion'];

        $list_sum3['total_pricex'] = (float)$this->zfreight;
        if(!empty($this->zfreight) && !empty($this->freightx_num1) && !empty($this->freightx_num2)){
            $list_sum3['freightx'] = (float)number_format((float)$this->zfreight / (float)$this->freightx_num1 * (float)$this->freightx_num2, 3, '.', '');
        }else{
            $list_sum3['freightx'] = 0;
        }
        if(!empty($this->freightx_num1) && !empty($this->zfreight)){
            $list_sum3['materialx'] = (float)number_format((float)$this->zfreight / (float)$this->freightx_num1, 3, '.', '');
        }else{
            $list_sum3['materialx'] = 0;
        }
        
        $list_sum3['commodity_type'] = $this->freightx_num2;
        $list_sum3['estimate_freight'] = '';
        $list_sum3['estimate_material'] = '';
        $list_sum3['admin_id'] = $admin_id;
        $list_sum3['b_time'] = $b_time;
        $list[] = $list_sum3;

        //同步列表数据
        $personreport['ytotal_price'] = $list_sum3['site_id'];
        $personreport['total_price'] = $list_sum3['name'];
        $personreport['sales_markup'] = $list_sum3['total_price'];
        $personreport['freight'] = $list_sum3['freight'];
        $personreport['material'] = $list_sum3['material'];
        $personreport['logistics_markup'] = $list_sum3['currency'];
        $personreport['advertising'] = $list_sum3['gateway'];
        $personreport['dispute_fees'] = $list_sum3['financial_status'];
        $personreport['xprofit'] = $list_sum3['fulfillment_status'];
        $personreport['sales_revenue'] = $list_sum3['tags'];
        $personreport['profit'] = $list_sum3['dispute_status'];

        table('shopify_personreport')
            ->where(['admin_id' => $admin_id])
            ->where(['timedate' => $b_time])
            ->update($personreport);

        $list[] = $list_sumb;

        //分新品跟老品
        $list_sum4 = array();
        $list_sum4['created_at'] = '热门店铺才有';
        $list_sum4['site_id'] = '新品';
        $list_sum4['name'] = $this->ztotal_price_new;
        $list_sum4['total_price'] = (float)number_format((float)$this->ztotal_price_new * 0.94, 3, '.', '');
        $list_sum4['freight'] = $this->zfreight_new;
        $list_sum4['material'] = $this->zmaterial_new;
        $list_sum4['refunded'] = '';
        $list_sum4['currency'] = $this->zfreight_addition_new;

        //查new_product
        $new_product = table('new_product')
                ->where(['admin_id' => $admin_id])
                ->where(['time' => $b_time])
                ->find();
        if(!empty($new_product)){
            $list_sum4['gateway'] = $new_product['advertising'];
        }else{
            $list_sum4['gateway'] = 0;
        }
        $list_sum4['financial_status'] = $this->disputez_new;
        $list_sum4['fulfillment_status'] = (float)number_format($list_sum4['total_price'] - $list_sum4['freight'] - $list_sum4['material'] - $list_sum4['currency'] - $list_sum4['gateway'] - $list_sum4['financial_status'], 3, '.', '');
        $list_sum4['tags'] = '';
        $list_sum4['dispute_status'] = '';
        $list_sum4['dispute_cost'] = '';
        $list_sum4['remarks'] = '';
        $list_sum4['total_pricex'] = '';
        $list_sum4['freightx'] = '';
        $list_sum4['materialx'] = '';
        $list_sum4['commodity_type'] = '';
        $list_sum4['estimate_freight'] = '';
        $list_sum4['estimate_material'] = '';
        $list_sum4['admin_id'] = $admin_id;
        $list_sum4['b_time'] = $b_time;
        $list[] = $list_sum4;

        // $list_sum5 = array();
        // $list_sum5['created_at'] = '热门店铺才有';
        // $list_sum5['site_id'] = '老品';
        // $list_sum5['name'] = $this->ztotal_price_old;
        // $list_sum5['total_price'] = (float)number_format((float)$this->ztotal_price_old * 0.94, 3, '.', '');
        // $list_sum5['freight'] = $this->zfreight_old;
        // $list_sum5['material'] = $this->zmaterial_old;
        // $list_sum5['currency'] = $this->zfreight_addition_old;
        // $list_sum5['gateway'] = 0;
        // $list_sum5['financial_status'] = $this->disputez_old;
        // $list_sum5['fulfillment_status'] = 0;
        // $list_sum5['tags'] = '';
        // $list_sum5['dispute_status'] = '';
        // $list_sum5['dispute_cost'] = '';
        // $list_sum5['remarks'] = '';
        // $list_sum5['total_pricex'] = '';
        // $list_sum5['freightx'] = '';
        // $list_sum5['materialx'] = '';
        // $list_sum5['commodity_type'] = '';
        // $list[] = $list_sum5;

        // $list_sum6 = array();
        // $list_sum6['created_at'] = '多人店铺才有';
        // $list_sum6['site_id'] = '新品+老品';
        // $list_sum6['name'] = $this->ztotal_price_main;
        // $list_sum6['total_price'] = (float)number_format((float)$this->ztotal_price_main * 0.94, 3, '.', '');
        // $list_sum6['freight'] = $this->zfreight_main;
        // $list_sum6['material'] = $this->zmaterial_main;
        // $list_sum6['currency'] = $this->zfreight_addition_main;
        // $list_sum6['gateway'] = 0;
        // $list_sum6['financial_status'] = $this->disputez_main;
        // $list_sum6['fulfillment_status'] = 0;
        // $list_sum6['tags'] = '';
        // $list_sum6['dispute_status'] = '';
        // $list_sum6['dispute_cost'] = '';
        // $list_sum6['remarks'] = '';
        // $list_sum6['total_pricex'] = '';
        // $list_sum6['freightx'] = '';
        // $list_sum6['materialx'] = '';
        // $list_sum6['commodity_type'] = '';
        // $list[] = $list_sum6;

        // $list[] = $list_sumb;

        // $list_sum7 = array();
        // $list_sum7['created_at'] = '多人店铺才有';
        // $list_sum7['site_id'] = '平分业绩';
        // $list_sum7['name'] = 0;
        // $list_sum7['total_price'] = 0;
        // $list_sum7['freight'] = 0;
        // $list_sum7['material'] = 0;
        // $list_sum7['currency'] = 0;
        // $list_sum7['gateway'] = 0;
        // $list_sum7['financial_status'] = 0;
        // $list_sum7['fulfillment_status'] = $user_gross;
        // $list_sum7['tags'] = '';
        // $list_sum7['dispute_status'] = '';
        // $list_sum7['dispute_cost'] = '';
        // $list_sum7['remarks'] = '';
        // $list_sum7['total_pricex'] = '';
        // $list_sum7['freightx'] = '';
        // $list_sum7['materialx'] = '';
        // $list_sum7['commodity_type'] = '';
        // $list[] = $list_sum7;

        // $list[] = $list_sumb;
        
        // $list_sum8 = array();
        // $list_sum8['created_at'] = '单人店铺';
        // $list_sum8['site_id'] = '';
        // $list_sum8['name'] = $this->ztotal_price_dan;
        // $list_sum8['total_price'] = (float)number_format((float)$this->ztotal_price_dan * 0.94, 3, '.', '');
        // $list_sum8['freight'] = $this->zfreight_dan;
        // $list_sum8['material'] = $this->zmaterial_dan;
        // $list_sum8['currency'] = $this->zfreight_addition_dan;
        // $list_sum8['gateway'] = 0;
        // $list_sum8['financial_status'] = $this->disputez_dan;
        // $list_sum8['fulfillment_status'] = 0;
        // $list_sum8['tags'] = '';
        // $list_sum8['dispute_status'] = '';
        // $list_sum8['dispute_cost'] = '';
        // $list_sum8['remarks'] = '';
        // $list_sum8['total_pricex'] = '';
        // $list_sum8['freightx'] = '';_new
        // $list_sum8['materialx'] = '';
        // $list_sum8['commodity_type'] = '';
        // $list[] = $list_sum8;

        //分新品跟老品
        if($admin_id == 31){
            $list[] = $list_sumb;

            // $list_sum9 = array();
            // $list_sum9['created_at'] = 'LilyBras USA';
            // $list_sum9['site_id'] = '邮件业绩';
            // $list_sum9['name'] = $this->ztotal_price_10;
            // $list_sum9['total_price'] = (float)number_format((float)$this->ztotal_price_10 * 0.94, 3, '.', '');
            // $list_sum9['freight'] = $this->zfreight_10;
            // $list_sum9['material'] = $this->zmaterial_10;
            // $list_sum9['refunded'] = '';
            // $list_sum9['currency'] = $this->zfreight_addition_10;
            // $list_sum9['gateway'] = 0;
            // $list_sum9['financial_status'] = $this->disputez_10;
            // $list_sum9['fulfillment_status'] = (float)number_format($list_sum9['total_price'] - $list_sum9['freight'] - $list_sum9['material'] - $list_sum9['currency'] - $list_sum9['gateway'] - $list_sum9['financial_status'], 3, '.', '');
            // $list_sum9['tags'] = '';
            // $list_sum9['dispute_status'] = '';
            // $list_sum9['dispute_cost'] = '';
            // $list_sum9['remarks'] = '';
            // $list_sum9['total_pricex'] = '';
            // $list_sum9['freightx'] = '';
            // $list_sum9['materialx'] = '';
            // $list_sum9['commodity_type'] = '';
            // $list_sum9['admin_id'] = $admin_id;
            // $list_sum9['b_time'] = $b_time;
            // $list[] = $list_sum9;

            $list_sum10 = array();
            $list_sum10['created_at'] = 'WooBilly Bras';
            $list_sum10['site_id'] = '邮件业绩';
            $list_sum10['name'] = $this->ztotal_price_29;
            $list_sum10['total_price'] = (float)number_format((float)$this->ztotal_price_29 * 0.94, 3, '.', '');
            $list_sum10['freight'] = $this->zfreight_29;
            $list_sum10['material'] = $this->zmaterial_29;
            $list_sum10['refunded'] = '';
            $list_sum10['currency'] = $this->zfreight_addition_29;
            $list_sum10['gateway'] = 0;
            $list_sum10['financial_status'] = $this->disputez_29;
            $list_sum10['fulfillment_status'] = (float)number_format($list_sum10['total_price'] - $list_sum10['freight'] - $list_sum10['material'] - $list_sum10['currency'] - $list_sum10['gateway'] - $list_sum10['financial_status'], 3, '.', '');
            $list_sum10['tags'] = '';
            $list_sum10['dispute_status'] = '';
            $list_sum10['dispute_cost'] = '';
            $list_sum10['remarks'] = '';
            $list_sum10['total_pricex'] = '';
            $list_sum10['freightx'] = '';
            $list_sum10['materialx'] = '';
            $list_sum10['commodity_type'] = '';
            $list_sum10['estimate_freight'] = '';
            $list_sum10['estimate_material'] = '';
            $list_sum10['admin_id'] = $admin_id;
            $list_sum10['b_time'] = $b_time;
            $list[] = $list_sum10;

            $list_sum11 = array();
            $list_sum11['created_at'] = 'new lilybras';
            $list_sum11['site_id'] = '邮件业绩';
            $list_sum11['name'] = $this->ztotal_price_20;
            $list_sum11['total_price'] = (float)number_format((float)$this->ztotal_price_20 * 0.94, 3, '.', '');
            $list_sum11['freight'] = $this->zfreight_20;
            $list_sum11['material'] = $this->zmaterial_20;
            $list_sum11['refunded'] = '';
            $list_sum11['currency'] = $this->zfreight_addition_20;
            $list_sum11['gateway'] = 0;
            $list_sum11['financial_status'] = $this->disputez_20;
            $list_sum11['fulfillment_status'] = (float)number_format($list_sum11['total_price'] - $list_sum11['freight'] - $list_sum11['material'] - $list_sum11['currency'] - $list_sum11['gateway'] - $list_sum11['financial_status'], 3, '.', '');
            $list_sum11['tags'] = '';
            $list_sum11['dispute_status'] = '';
            $list_sum11['dispute_cost'] = '';
            $list_sum11['remarks'] = '';
            $list_sum11['total_pricex'] = '';
            $list_sum11['freightx'] = '';
            $list_sum11['materialx'] = '';
            $list_sum11['commodity_type'] = '';
            $list_sum11['estimate_freight'] = '';
            $list_sum11['estimate_material'] = '';
            $list_sum11['admin_id'] = $admin_id;
            $list_sum11['b_time'] = $b_time;
            $list[] = $list_sum11;
        }
        
        return $list;
    }
    
    /**
    * 方法详情
    * @param array params
    */
    public function calculate_new_product_labels($item)
    {
        // 优先分配手动新品标签
        if($item['new_product'] == 'FBNEW'){
            // 新品
            $this->ztotal_price_new = (float)number_format((float)$item['total_pricex'] + $this->ztotal_price_new, 3, '.', '');
            $this->zfreight_new = (float)number_format((float)$item['freightx'] + $this->zfreight_new, 3, '.', '');
            $this->zmaterial_new = (float)number_format((float)$item['materialx'] + $this->zmaterial_new, 3, '.', '');
            if($item['fulfillment_status'] == 'fulfilled'){
                $this->zfreight_addition_new += (float)number_format(2 / 7.150, 3, '.', '');
            }
            $this->disputez_new = (float)number_format((float)$item['dispute_cost'] + $this->disputez_new, 3, '.', '');

            $item['commodity_type'] = '新品';
        }else{
            $pos = strpos($item['tags'], 'FBNEW');
            if($pos !== false){
                // 新品
                $this->ztotal_price_new = (float)number_format((float)$item['total_pricex'] + $this->ztotal_price_new, 3, '.', '');
                $this->zfreight_new = (float)number_format((float)$item['freightx'] + $this->zfreight_new, 3, '.', '');
                $this->zmaterial_new = (float)number_format((float)$item['materialx'] + $this->zmaterial_new, 3, '.', '');
                if($item['fulfillment_status'] == 'fulfilled'){
                    $this->zfreight_addition_new += (float)number_format(2 / 7.150, 3, '.', '');
                }
                $this->disputez_new = (float)number_format((float)$item['dispute_cost'] + $this->disputez_new, 3, '.', '');

                $item['commodity_type'] = '新品';
            }else{
                // 老品
                $this->ztotal_price_old = (float)number_format((float)$item['total_pricex'] + $this->ztotal_price_old, 3, '.', '');
                $this->zfreight_old = (float)number_format((float)$item['freightx'] + $this->zfreight_old, 3, '.', '');
                $this->zmaterial_old = (float)number_format((float)$item['materialx'] + $this->zmaterial_old, 3, '.', '');
                if($item['fulfillment_status'] == 'fulfilled'){
                    $this->zfreight_addition_old += (float)number_format(2 / 7.150, 3, '.', '');
                }
                $this->disputez_old = (float)number_format((float)$item['dispute_cost'] + $this->disputez_old, 3, '.', '');

                $item['commodity_type'] = '老品';
            }
        }

        
        return $item;
    }
    
}
